package com.imau.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imau.bean.Teacher;
import com.imau.common.entiry.PageQuery;
import com.imau.common.entiry.ResultCode;
import com.imau.common.entiry.ResultEntity;
import com.imau.common.utils.JSONUtil;
import com.imau.service.TeacherService;
import com.imau.service.imp.TeacherServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name = "TeacherConterllor", value = "/teacher/*")
public class TeacherConterllor extends Baseconterllor {

    public TeacherConterllor() {

        this.service = new TeacherServiceImp();
    }

    /**添加教师*/
    public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        //1.获取前端
        BufferedReader reader = request.getReader();

        String jsonObj = reader.readLine();

        //2.转换
        Teacher teacher = JSONUtil.jsonToEntity(jsonObj,Teacher.class);

        //3.调用服务

        boolean b = service.saveEntity(teacher);

        //4.响应数据

        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {


        //1.获取前台主键id
        String id = request.getParameter("id");

        //2.业务处理

        boolean b = service.removeWithById(id);

        //3.响应数据


        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));

    }


    public void update (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {

        //1.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();

        Teacher teacher = JSONUtil.jsonToEntity(jsonStr,Teacher.class);
        //2.调用service方法添加

        boolean b = service.updateByEntity(teacher);

        //3.响应数据

        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));


    }


    public void getByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {


        //1.获取前台分页数据
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize =  Integer.valueOf(request.getParameter("pageSize"));

        //2.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();


        //3.转成对象

        Teacher teacher = JSONUtil.jsonToEntity(jsonStr,Teacher.class);


        //4.调用service方法添加

        Page<Teacher> page = service.queryPageList(teacher,new PageQuery(pageNum,pageSize));

        //3.响应数据

        this.responseBody(response,page==null?false:true,new ResultEntity(ResultCode.SUCCESS,page));


    }


    public void getInfo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {


        //1.获取前台主键id
        String id = request.getParameter("id");

        //2.业务处理

        Teacher teacher =((TeacherService)service).queryById(id);

        //3.响应数据


        this.responseBody(response,teacher!=null?true:false,new ResultEntity(ResultCode.SUCCESS,teacher));

    }
}
